import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isLogin: true,
    // 用户信息
    user: {
      username: '', // 用户名
      nickname: '', // 昵称
      balance: 0, // 余额
      txcolor: '', // 头像文字颜色
      bgcolor: '', // 头像背景颜色
      // 收货人地址
      consignees: [
        // {
        //   id: 1,
        //   username: 'smt',
        //   address: '福建龙岩市新罗区城区',
        //   phone: '17526555470'
        // },
        // {
        //   id: 2,
        //   username: '光头强',
        //   address: '奇迹森林狗熊岭强子的小木屋',
        //   phone: '17556448477'
        // }
      ]

    },
    // 当前定位（位置）
    address: null
  },
  mutations: {
    // 获取用户信息
    setUserInfo (state, data) {
      var user = data.users[0]

      state.user.username = user.username
      state.user.nickname = user.nickname === null ? user.username : user.nickname
      state.user.balance = user.balance
      state.user.txcolor = user.txcolor
      state.user.bgcolor = user.bgcolor

      data.addressList.forEach(item => {
        state.user.consignees.push({
          id: item.id,
          username: item.consignee,
          address: item.cPhone,
          phone: item.shippingAddress
        })
      })
    },
    // 修改当前定位
    setLocation (state, data) {
      state.address = data
    },
    // 修改昵称
    setNickname (state, nickname) {
      state.user.nickname = nickname
    },
    // 修改收货地址
    setConsignee (state, data) {
      state.user.consignees.forEach(item => {
        if (item.id === data.id) {
          item.username = data.consignee.username
          item.address = data.consignee.address
          item.phone = data.consignee.phone
        }
      })
    },
    // 添加收货地址
    handleAdd (state, data) {
      data.id = state.user.consignees.length
      state.user.consignees.push(data)
    },
    // 修改登录状态
    handleLogin (state, isState) {
      state.isLogin = isState
    }

  },
  actions: {
  },
  modules: {
  }
})
